package br.com.hbt.expressaoregular;

import java.util.Formatter;
import java.util.Locale;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExpressaoRegular {

    public static void main(String[] args) {

        Pattern p1 = Pattern.compile("\\d*");
        Matcher m1 = p1.matcher("ab34ef");
        boolean b = false;
        while (b = m1.find()) {
            //            System.out.print(m1.start() + " " + m1.group());
        }

        Scanner s = new Scanner("1 2 a 3 45 6");
        int x = 0;
        do {
            x = s.nextInt();
            System.out.println(x + " ");
        } while (x != 0);

        //        testesGerais();
    }

    public static void testesGerais() {
        //      System.out.printf("12%1$s", "13%1$s", "14%1$s");
        System.out.printf("12%1$20s", "13%1$s", "14%1$s");

        StringBuilder sb = new StringBuilder();
        // Send all output to the Appendable object sb
        Formatter formatter = new Formatter(sb, Locale.US);

        System.out.println(formatter);
        // Explicit argument indices may be used to re-order output.
        formatter.format("%4$5s %3$5s %2$5s %1$5s", "a", "b", "c", "d");
        // -> " d  c  b  a"
        System.out.println(formatter);
        // Optional locale as the first argument can be used to get
        // locale-specific formatting of numbers.  The precision and width can be
        // given to round and align the value.
        formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E);
        // -> "e =    +2,7183"

        System.out.println(formatter);
        // The '(' numeric flag may be used to format negative numbers with
        // parentheses rather than a minus sign.  Group separators are
        // automatically inserted.
        formatter.format("Amount gained or lost since last statement: $ %(,.2f", new Float("6.217"));
        // -> "Amount gained or lost since last statement: $ (6,217.58)"
        System.out.println(formatter);

        //System.out.println(getSelectHtml());

        String padrao = "Java";
        String texto = "java";
        System.out.println(texto.matches(padrao));

        /**
         * MODIFICADORES
         * 
         * (?i), Ignora maiuscula e minuscula
         * (?x), Comentários
         * (?m), Multilinhas
         * (?s), Dottal
         */

        boolean b = false;

        b = "Java".matches("(?im)java");
        System.out.println(b);

        /**
         * METACARACTERES
         * . qualquer caracter
         * \d digitos [0-9]
         * \D não é digito [^0-9]
         * \s espaços [ \t\n\x0b\f\r]
         * \S não é espaço [^\s]
         * \w letra [a-zA-Z_0-9]
         * \W não é letra
         */

        b = "8 8 ".matches("\\d\\s\\d\\s");
        System.out.println(b);

        boolean bol = "88130-760".matches("\\d\\d\\d\\d\\d-\\d\\d\\d");
        System.out.println(bol);

        /**
         * QUANTIFICADORES
         * X{n} X, exatamente n vezes
         * X{n,} X, pelo menos n vezes
         * X{n, m} X, pelo menos n vezes, mas não mais do que m vezes
         * X? X, 0 ou 1 vez
         * X* X, 0 ou + vezes
         * X+ X, 1 ou + vezes
         */

        // X{n} X, exatamente n vezes
        bol = "60".matches("\\d{2}");
        System.out.println(bol);

        // X{n,} X, pelo menos n vezes
        bol = "11".matches("\\d{2,}");
        System.out.println(bol);

        // X{n, m} X, pelo menos n vezes, mas não mais do que m vezes
        bol = "12345".matches("\\d{2,5}");
        System.out.println(bol);

        // X?   X, 0 ou 1 vez
        bol = "a".matches(".?");
        System.out.println(bol);

        //X*    X, 0 ou + vezes
        bol = "d8ad".matches(".*");
        System.out.println(bol);

        //X+    X, 1 ou + vezes
        bol = "a34q34".matches(".+");
        System.out.println(bol);

        bol = "88130-760".matches("\\d{5}-\\d{3}");
        System.out.println(bol);

        bol = "29/12/1982".matches("\\d{2}/\\d{2}/\\d{4}");
        System.out.println(bol);

        /**
         * METACARACTERER DE FRONTEIRA
         * ^ inicia
         * $ finaliza
         * | ou
         */

        bol = "hbt32".matches("^hbt.*");
        System.out.println(bol);

        bol = "hbt32".matches(".*32$");
        System.out.println(bol);

        bol = "tem java aqui".matches("^.*java.*");
        System.out.println(bol);

        bol = "tem java aqui".matches("^tem.*aqui$");
        System.out.println(bol);

        bol = "sim".matches("sim|nao");
        System.out.println(bol);

        /**
         * AGRUPADORES
         * [...] Agrupamento
         * [a-z] Alcance
         * [a-e][i-u] União
         * [a-z&&[aeiou]] Interseção
         * [^abc] Exceção
         * [a-z&&[^m-p]] Subtração
         * \x Fuga literal
         */

        //[a-z] Alcance
        bol = "a".matches("[a-z]");
        System.out.println(bol);

        //[0-9] Alcance
        bol = "5".matches("[1-5]");
        System.out.println(bol);

        bol = "yes".matches("[tT]rue|[yY]es");
        System.out.println(bol);

        //Valição de primeiro nome:
        bol = "Herbert".matches("[A-Z][a-z]*");
        System.out.println(bol);

        bol = "olho".matches("[^abc]lho");
        System.out.println(bol);

        bol = "creu".matches("cr[ae]u");
        System.out.println(bol);

        bol = "hbtvieira@gmail.com".matches("\\w+@\\w+\\.\\w{2,3}");
        System.out.println(bol);

        String doce = "Qual é o doce mais doCe que o doce?";

        Matcher matcher = Pattern.compile("(?i)doce").matcher(doce);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
        //        System.out.println("matcher.group: " + matcher.group());

        Pattern pt = Pattern.compile("Certified");
        Matcher mt = pt.matcher("Sun Certified 1 Java Programmer Certified 2");
        mt.find();

        System.out.println("mt.group: " + mt.group());

        Pattern.compile("H[a-d][k-m]");
        Pattern compile = Pattern.compile("H[a-b][k-m]");
        Matcher matcher2 = compile.matcher("Hallo Hallo");
        matcher2.find();
        System.out.println("Matcher EXAMLABS: " + matcher2.group());

        String sv = "Contact ExamLab customer service: cs@examlab.tk Thank you";
        String s1 = sv.split("[@.]")[1];
        System.out.println("###AAAAA##: " + s1);
        String s2 = sv.split("(.)*[@]")[1];
        System.out.println("###BBBBB##: " + s2);
        //        String s3 = sv.split("(.)*@(.)*com")[1];
        //        System.out.println("###CCCCC##: " + s3);
        //        String s4 = sv.split("@[(\\w)+].")[1];
        //        System.out.println("###DDDDD##: " + s4);
        //        String s5 = sv.split("  ")[1];
        //        System.out.println("###EEEEE##: " + s5);

        /**
         * SUBSTITUIÇÕES
         */

        String r = doce.replaceAll("(?i)doce", "DOCINHO");
        System.out.println(r);

        String rato = "O rato roeu a roupa do rei de roma";

        r = rato.replaceAll("r[aeiou]", "XX");
        System.out.println(r);

        r = "Tabular esse texto".replaceAll("\\s", "\t");
        System.out.println(r);

        String url = "www.xti.com.br/clientes-2011.html";
        //http://www.xti.com.br/2011/clientes.jsp

        String re = "www.xti.com.br/\\w{2,}-\\d{4}.html";
        b = url.matches(re);
        System.out.println(b);

        re = "(www.xti.com.br)/(\\w{2,})-(\\d{4}).html";

        r = url.replaceAll(re, "http://$1/$3/$2\\.jsp");
        System.out.println(r);

        /**
         * MODIFICADORES-------------------------|------METACARACTERES--------------|-----QUANTIFICADORES
         * (?i), Ignora maiuscula e minuscula----|------. qualquer caracter---------|-----X{n} X, exatamente n vezes
         * (?x), Comentários---------------------|------\d digitos [0-9]------------|-----X{n,} X, pelo menos n vezes
         * (?m), Multilinhas---------------------|------\D não é digito [^0-9]------|-----X{n, m} X, pelo menos n vezes, mas não mais do que m vezes
         * (?s), Dottal--------------------------|------\s espaços [ \t\n\x0b\f\r]--|-----X? X, 0 ou 1 vez
         * --------------------------------------|------\S não é espaço [^\s]-------|-----X* X, 0 ou + vezes
         * --------------------------------------|------\w letra [a-zA-Z_0-9]-------|-----X+ X, 1 ou + vezes
         * --------------------------------------|------\W não é letra
         ** ==========================================================================================================================================
         * AGRUPADORES------------------------|------------METACARACTERER DE FRONTEIRA
         * [...] Agrupamento------------------|------------^ inicia
         * [a-z] Alcance----------------------|------------$ finaliza
         * [a-e][i-u] União-------------------|------------| ou
         * [a-z&&[aeiou]] Interseção----------|------------
         * [^abc] Exceção---------------------|------------
         * [a-z&&[^m-p]] Subtração------------|------------
         * \x Fuga literal--------------------|------------
         */

        System.out.println("=================");
        System.out.println("hbt.vi3ira@gmail.com".matches("\\w.*@\\w*\\.\\w*"));
        System.out.println("Now we all can Learn java.".matches("^.*[A-Z][a-z]{4}.*"));
        String principal = "Now we all can Learn java.";
        String nova = principal.replaceAll("[A-Z][a-z]{4}", "Fuck");
        System.out.println(nova);

        String[] valores =
                getSelectHtml().replaceAll("<select.*;\">", "")
                               .replaceAll("<option value=\"[0-9]*\">", "")
                               .replaceAll("</option>", "|")
                               .replaceAll("</select>", "").split("\\|", -1);
        for (String string : valores) {
            if (!string.isEmpty()) {
                //                System.out.println(string);
            }
        }

        Pattern p = Pattern.compile("aba");
        Matcher m = p.matcher("abababa");
        while (m.find()) {
            //            System.out.println(m.start() + " ");
        }

        p = Pattern.compile("\\d");
        m = p.matcher("a12c3e456f");
        while (m.find()) {
            System.out.println(m.start() + " " + m.group());
        }

        //        testScanner(args[0]);
        System.out.printf("%1$(+123d", -123);
    }

    public static void testScanner(String teste) {
        System.out.print("Input: ");
        System.out.flush();
        try {
            Scanner scan = new Scanner(System.in);
            String token;
            do {
                token = scan.findInLine(teste);
                System.out.println("Found: " + token);
            } while (token != null);

        } catch (Exception e2) {
            System.out.println("Scan Exception");
        }
    }

    public static String getSelectHtml() {
        return
        "<select class=\"span-5 last raio-5\" friendly=\"true\" id=\"IdMarca\" name=\"IdMarca\" selected=\"selected\" style=\"visibility: visible;\">"
                + "<option value=\"\"></option>"
                + "<option value=\"0\"></option>"
                + "<option value=\"2\">CHEVROLET</option>"
                + "<option value=\"4\">FIAT</option>"
                + "<option value=\"3\">FORD</option>"
                + "<option value=\"16\">HONDA</option>"
                + "<option value=\"17\">HYUNDAI</option>"
                + "<option value=\"26\">MITSUBISHI</option>"
                + "<option value=\"28\">PEUGEOT</option>"
                + "<option value=\"30\">RENAULT</option>"
                + "<option value=\"35\">TOYOTA</option>"
                + "<option value=\"5\">VOLKSWAGEN</option>"
                + "<option value=\"0\"></option>"
                + "<option value=\"167\">ACURA</option>"
                + "<option value=\"237\">ADAMO</option>"
                + "<option value=\"223\">AGRALE</option>"
                + "<option value=\"6\">ALFA ROMEO</option>"
                + "<option value=\"232\">AMERICAR</option>"
                + "<option value=\"7\">ASIA</option>"
                + "<option value=\"239\">ASTON MARTIN</option>"
                + "<option value=\"8\">AUDI</option>"
                + "<option value=\"225\">AUSTIN</option>"
                + "<option value=\"227\">BAJA</option>"
                + "<option value=\"188\">BEACH</option>"
                + "<option value=\"9\">BENTLEY</option>"
                + "<option value=\"179\">BIANCO</option>"
                + "<option value=\"10\">BMW</option>"
                + "<option value=\"290\">BORGWARD </option>"
                + "<option value=\"289\">BRILLIANCE</option>"
                + "<option value=\"162\">BRM</option>"
                + "<option value=\"251\">BUGATTI</option>"
                + "<option value=\"176\">BUGWAY</option>"
                + "<option value=\"119\">BUICK</option>"
                + "<option value=\"120\">CADILLAC</option>"
                + "<option value=\"219\">CBT</option>"
                + "<option value=\"122\">CHAMONIX</option>"
                + "<option value=\"241\">CHANA</option>"
                + "<option value=\"292\">CHANGAN</option>"
                + "<option value=\"271\">CHEDA</option>"
                + "<option value=\"270\">CHERY</option>"
                + "<option value=\"2\">CHEVROLET</option>"
                + "<option value=\"11\">CHRYSLER</option>"
                + "<option value=\"12\">CITROËN</option>"
                + "<option value=\"236\">CORD</option>"
                + "<option value=\"244\">COYOTE</option>"
                + "<option value=\"171\">CROSS LANDER</option>"
                + "<option value=\"13\">DAEWOO</option>"
                + "<option value=\"14\">DAIHATSU</option>"
                + "<option value=\"245\">DE SOTO</option>"
                + "<option value=\"285\">DE TOMASO </option>"
                + "<option value=\"286\">DELOREAN</option>"
                + "<option value=\"177\">DKW-VEMAG</option>"
                + "<option value=\"102\">DODGE</option>"
                + "<option value=\"233\">DUNNAS</option>"
                + "<option value=\"267\">EAGLE</option>"
                + "<option value=\"243\">EFFA</option>"
                + "<option value=\"247\">EMIS</option>"
                + "<option value=\"123\">ENGESA</option>"
                + "<option value=\"166\">ENVEMO</option>"
                + "<option value=\"250\">FARUS</option>"
                + "<option value=\"260\">FERCAR BUGGY</option>"
                + "<option value=\"15\">FERRARI</option>"
                + "<option value=\"4\">FIAT</option>"
                + "<option value=\"266\">FIBRAVAN</option>"
                + "<option value=\"230\">FNM</option>"
                + "<option value=\"3\">FORD</option>"
                + "<option value=\"249\">FYBER</option>"
                + "<option value=\"304\">GEELY</option>"
                + "<option value=\"126\">GEO</option>"
                + "<option value=\"128\">GMC</option>"
                + "<option value=\"238\">GRANCAR </option>"
                + "<option value=\"264\">GREAT WALL</option>"
                + "<option value=\"130\">GURGEL</option>"
                + "<option value=\"257\">HAFEI</option>"
                + "<option value=\"248\">HB</option>"
                + "<option value=\"253\">HOFSTETTER</option>"
                + "<option value=\"16\">HONDA</option>"
                + "<option value=\"279\">HUDSON</option>"
                + "<option value=\"115\">HUMMER</option>"
                + "<option value=\"17\">HYUNDAI</option>"
                + "<option value=\"275\">INCOFER</option>"
                + "<option value=\"183\">INFINITI</option>"
                + "<option value=\"184\">INTERNATIONAL</option>"
                + "<option value=\"132\">ISUZU</option>"
                + "<option value=\"133\">IVECO</option>"
                + "<option value=\"287\">JAC</option>"
                + "<option value=\"18\">JAGUAR</option>"
                + "<option value=\"218\">JEEP</option>"
                + "<option value=\"301\">JENSEN</option>"
                + "<option value=\"258\">JINBEI</option>"
                + "<option value=\"20\">JPX</option>"
                + "<option value=\"291\">KAISER</option>"
                + "<option value=\"21\">KIA</option>"
                + "<option value=\"252\">KOENIGSEGG</option>"
                + "<option value=\"254\">L AUTOMOBILE</option>"
                + "<option value=\"263\">L´AUTO CRAFT</option>"
                + "<option value=\"42\">LADA</option>"
                + "<option value=\"22\">LAMBORGHINI</option>"
                + "<option value=\"185\">LANCIA</option>"
                + "<option value=\"23\">LAND ROVER</option>"
                + "<option value=\"303\">LANDWIND</option>"
                + "<option value=\"117\">LEXUS</option>"
                + "<option value=\"280\">LIFAN</option>"
                + "<option value=\"135\">LINCOLN</option>"
                + "<option value=\"220\">LOBINI</option>"
                + "<option value=\"136\">LOTUS</option>"
                + "<option value=\"246\">MAHINDRA</option>"
                + "<option value=\"172\">MARCOPOLO</option>"
                + "<option value=\"161\">MARINA´S</option>"
                + "<option value=\"111\">MASERATI</option>"
                + "<option value=\"169\">MATRA</option>"
                + "<option value=\"262\">MAYBACH</option>"
                + "<option value=\"24\">MAZDA</option>"
                + "<option value=\"298\">MCLAREN</option>"
                + "<option value=\"259\">MENON</option>"
                + "<option value=\"25\">MERCEDES-BENZ</option>"
                + "<option value=\"137\">MERCURY</option>"
                + "<option value=\"150\">MG</option>"
                + "<option value=\"180\">MINI</option>"
                + "<option value=\"26\">MITSUBISHI</option>"
                + "<option value=\"138\">MIURA</option>"
                + "<option value=\"229\">MORRIS</option>"
                + "<option value=\"216\">MP LAFER</option>"
                + "<option value=\"274\">MPLM</option>"
                + "<option value=\"284\">NEWTRACK</option>"
                + "<option value=\"27\">NISSAN</option>"
                + "<option value=\"141\">OLDSMOBILE</option>"
                + "<option value=\"234\">OPEL</option>"
                + "<option value=\"297\">PACKARD</option>"
                + "<option value=\"261\">PAG</option>"
                + "<option value=\"269\">PAGANI</option>"
                + "<option value=\"28\">PEUGEOT</option>"
                + "<option value=\"142\">PLYMOUTH</option>"
                + "<option value=\"143\">PONTIAC</option>"
                + "<option value=\"29\">PORSCHE</option>"
                + "<option value=\"144\">PUMA</option>"
                + "<option value=\"299\">RELY</option>"
                + "<option value=\"30\">RENAULT</option>"
                + "<option value=\"255\">RENO</option>"
                + "<option value=\"281\">REVA-I</option>"
                + "<option value=\"186\">ROLLS-ROYCE</option>"
                + "<option value=\"214\">ROMI</option>"
                + "<option value=\"145\">ROVER</option>"
                + "<option value=\"146\">SAAB</option>"
                + "<option value=\"147\">SANTA MATILDE</option>"
                + "<option value=\"170\">SATURN</option>"
                + "<option value=\"31\">SEAT</option>"
                + "<option value=\"178\">SHELBY</option>"
                + "<option value=\"295\">SHINERAY</option>"
                + "<option value=\"231\">SHORT</option>"
                + "<option value=\"148\">SIMCA</option>"
                + "<option value=\"226\">SMART</option>"
                + "<option value=\"272\">SPYKER</option>"
                + "<option value=\"32\">SSANGYONG</option>"
                + "<option value=\"278\">STANDARD</option>"
                + "<option value=\"217\">STUDEBAKER</option>"
                + "<option value=\"33\">SUBARU</option>"
                + "<option value=\"34\">SUZUKI</option>"
                + "<option value=\"276\">TAC</option>"
                + "<option value=\"282\">TANGER</option>"
                + "<option value=\"302\">TESLA</option>"
                + "<option value=\"35\">TOYOTA</option>"
                + "<option value=\"268\">TRIUMPH</option>"
                + "<option value=\"153\">TROLLER</option>"
                + "<option value=\"235\">UNIMOG</option>"
                + "<option value=\"5\">VOLKSWAGEN</option>"
                + "<option value=\"36\">VOLVO</option>"
                + "<option value=\"265\">WAKE</option>"
                + "<option value=\"240\">WALK</option>"
                + "<option value=\"273\">WAY BRASIL</option>"
                + "<option value=\"296\">WIESMANN</option>"
                + "<option value=\"213\">WILLYS</option>"
                + "<option value=\"288\">WILLYS OVERLAND</option>"
                + "</select>";
    }
}